#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>

/*
 * int sched_get_priority_max(int policy);
 * 
 * int sched_get_priority_min(int policy);
 * 
 * DESCRIPTION
 * sched_get_priority_max()  returns  the  maximum priority value that can be used with the scheduling algorithm identified by policy.  sched_get_priority_min() returns
 * the minimum priority value that can be used with the scheduling algorithm identified by policy.  Supported policy values are SCHED_FIFO, SCHED_RR,  SCHED_OTHER,  and
 * SCHED_BATCH.  Further details about these policies can be found in sched_setscheduler(2).
 * 
 * Processes  with numerically higher priority values are scheduled before processes with numerically lower priority values.  Thus, the value returned by sched_get_pri-
 * ority_max() will be greater than the value returned by sched_get_priority_min().
 * 
 * Linux allows the static priority value range 1 to 99 for SCHED_FIFO and SCHED_RR and the priority 0 for SCHED_OTHER and SCHED_BATCH.  Scheduling priority ranges  for
 * the various policies are not alterable.
 * 
 * The  range  of  scheduling priorities may vary on other POSIX systems, thus it is a good idea for portable applications to use a virtual priority range and map it to
 * the interval given by sched_get_priority_max() and sched_get_priority_min().  POSIX.1-2001 requires a spread of at least 32 between the maximum and the minimum  val-
 * ues for SCHED_FIFO and SCHED_RR.
 * 
 * POSIX systems on which sched_get_priority_max() and sched_get_priority_min() are available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.
 * 
 * RETURN VALUE
 * On  success,  sched_get_priority_max()  and  sched_get_priority_min()  return  the  maximum/minimum  priority value for the named scheduling policy.  On error, -1 is
 * returned, and errno is set appropriately.
 * 
 * ERRORS
 * EINVAL The argument policy does not identify a defined scheduling policy.
 */


/*
 * for kernel 2.6.34
 *  * Scheduling policies
 *   */
/*
#define SCHED_NORMAL		0
#define SCHED_FIFO		1
#define SCHED_RR		2
#define SCHED_BATCH		3
*/
/* SCHED_ISO: reserved but not implemented yet */
/*
#define SCHED_IDLE		5
*/
/* Can be ORed in to make sure the process is reverted back to SCHED_NORMAL on fork */
/*
#define SCHED_RESET_ON_FORK     0x40000000
*/


int main(int argc, char * argv[])
{
	if(argc != 2)
	{
		printf("Usgae: %s <policy>\n", argv[0]);
		exit(1);
	}
	int prio_max = sched_get_priority_max(atoi(argv[1]));
	if(-1 == prio_max)
	{
		switch(errno)
		{
			case EINVAL:
			       printf("The argument policy does not identify a defined scheduling policy.\n");
			       break;
			default:
			       break;
		}
		return 1;
	}
	else
	{
		printf("prio_max: %d\n", prio_max);
		return 0;
	}
}
